(function () {
    var that=null;
    function Game(map) {
        this.food=new Food();
        this.snake=new Snake();
        this.map=map;
        that=this;
    }
    Game.prototype.start=function () {
        this.food.render(this.map);
        this.snake.render(this.map);
        runSnake();

function runSnake() {
    var headX;
    var maxX;
    var maxY;
    var headY;
    var timeId=setInterval(function (args) {
        that.snake.move(that.food,that.map);
        that.snake.render(that.map);

         headX=that.snake.body[0].x;
         maxX =that.map.offsetWidth/that.snake.width;
         maxY =that.map.offsetHeight/that.snake.height;
         headY=that.snake.body[0].y;


        if(headX<0||headX>=maxX){
            alert('low');
            clearInterval(timeId);
        }
        if(headY<0||headY>=maxY){
            alert('low');
            clearInterval(timeId);
         }
         // console.log(headX,headY,maxX,maxY);
    },100);
}
document.addEventListener('keydown',function (ev) {
    switch(ev.keyCode){
        case 37:that.snake.direction='left';break;
        case 38:that.snake.direction='top';break;
        case 39:that.snake.direction='right';break;
        case 40:that.snake.direction='bottom';break;
    }
},false)
        // this.snake.move();
        // this.snake.render(this.map);
        // this.snake.move();
        // this.snake.render(this.map);
        // this.snake.move();
        // this.snake.render(this.map);

    };
    window.Game=Game;
})();